package com.ruoyi.web.controller.pv;

import java.awt.*;
import java.io.File;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.pv.domain.PvClassScore;
import com.ruoyi.pv.domain.PvHomeImage;
import com.ruoyi.pv.service.IPvClassScoreService;
import com.ruoyi.pv.service.IPvHomeImageService;
import com.ruoyi.pv.service.IPvStudentRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pv.domain.PvTrainingClass;
import com.ruoyi.pv.service.IPvTrainingClassService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 培训班管理Controller
 *
 * @author zzx
 * @date 2023-03-12
 */
@Api("培训班管理")
@RestController
@RequestMapping("/pv/training_class")
public class PvTrainingClassController extends BaseController {
    @Autowired private IPvTrainingClassService pvTrainingClassService;
    @Autowired private IPvHomeImageService homeImageService;


    /**
     * 查询培训班管理列表
     */
    @GetMapping("/list")
    @ApiOperation("查询培训班管理列表")
    public TableDataInfo list(PvTrainingClass pvTrainingClass) {
        pvTrainingClass.setReservedOne(getDeptId());
        startPage();
        List<PvTrainingClass> list = pvTrainingClassService.selectPvTrainingClassList(pvTrainingClass);
        return getDataTable(list);
    }

    /**
     * 查询今年的第一个培训班
     */
    @GetMapping("/class-year-first")
    @ApiOperation("查询今年的最后一个培训班")
    public AjaxResult classYearFirst() {
        PvTrainingClass clazz = pvTrainingClassService.selectPvTrainingYearLast(getDeptId());
        //查询最近的一张图片
        return AjaxResult.success(clazz).put("homeImage",homeImageService.url());
    }


    /**
     * 导出培训班管理列表
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:export')")
    @Log(title = "培训班管理" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PvTrainingClass pvTrainingClass) {
        pvTrainingClass.setReservedOne(getDeptId());
        List<PvTrainingClass> list = pvTrainingClassService.selectPvTrainingClassList(pvTrainingClass);
        ExcelUtil<PvTrainingClass> util = new ExcelUtil<PvTrainingClass>(PvTrainingClass.class);
        util.exportExcel(response, list, "培训班管理数据");
    }

    /**
     * 获取培训班管理详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation("获取培训班管理详细信息")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(pvTrainingClassService.selectPvTrainingClassById(id));
    }

    /**
     * 新增培训班管理
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:add')")
    @Log(title = "培训班管理" , businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增培训班管理")
    public AjaxResult add(@RequestBody PvTrainingClass pvTrainingClass) {
        pvTrainingClass.setReservedOne(getDeptId());
        return toAjax(pvTrainingClassService.insertPvTrainingClass(pvTrainingClass));
    }

    /**
     * 修改培训班管理
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:edit')")
    @Log(title = "培训班管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改培训班管理")
    public AjaxResult edit(@RequestBody PvTrainingClass pvTrainingClass) {
        return toAjax(pvTrainingClassService.updatePvTrainingClass(pvTrainingClass));
    }

    /**
     * 删除培训班管理
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:remove')")
    @Log(title = "培训班管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除培训班管理")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pvTrainingClassService.deletePvTrainingClassByIds(ids));
    }

    /**
     * 生成邀请二维码
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:QRCode')")
    @Log(title = "生成邀请二维码" , businessType = BusinessType.EXPORT)
    @GetMapping("/QRCode")
    @ApiOperation("生成邀请二维码")
    public AjaxResult QRCode() {
        QrConfig config = new QrConfig(300, 300);
        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.CYAN);
        // 设置背景色（灰色）
        config.setBackColor(Color.GRAY);
        String base64 = QrCodeUtil.generateAsBase64("https://www.baidu.com", config, "png");
        return AjaxResult.success((Object) base64);
    }

    /**
     * 查询发送短信的人数
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:edit')")
    @Log(title = "发送短信通知-人数" , businessType = BusinessType.EXPORT)
    @GetMapping("/smsSend-people-count")
    public AjaxResult smsSendPeopleCount() {
        return AjaxResult.success(pvTrainingClassService.smsSendPeopleCount());
    }

    /**
     * 发送短信
     */
    @PreAuthorize("@ss.hasPermi('pv:training_class:edit')")
    @Log(title = "发送短信通知" , businessType = BusinessType.EXPORT)
    @GetMapping("/smsSend/{id}")
    public AjaxResult smsSend(@PathVariable("id")Long id) {
        pvTrainingClassService.smsSend(id);
        return AjaxResult.success();
    }
}
